import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import initNewInfo from "./functions/initNewInfo";
import beforeFormEdit from "./functions/beforeFormEdit";
import afterFormEdit from "./functions/afterFormEdit";
import afterPsnSelect from "./functions/afterPsnSelect";
import saveNewInfo from "./functions/saveNewInfo";

const {NCModal, NCButton, NCCheckbox} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class AddNew extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isSynWork: false
        };
        this.getEditInitInfo = this.getEditInitInfo.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.beSure = this.beSure.bind(this);
        this.onBeforeAddFormEvent = this.onBeforeAddFormEvent.bind(this);
        this.onAfterAddFormEvent = this.onAfterAddFormEvent.bind(this);
        this.onBeforePartFormEvent = this.onBeforePartFormEvent.bind(this);
        this.onAfterPartFormEvent = this.onAfterPartFormEvent.bind(this);
    }

    getEditInitInfo() {
        const {form} = this.props;
        form.setFormStatus('partadd', 'edit');
        form.setFormStatus('parttime', 'edit');
        initNewInfo.call(this);
    }

    beSure() {
        const flag = this.props.form.isCheckNow('partadd') && this.props.form.isCheckNow('parttime');
        if (!flag) return;
        saveNewInfo.call(this)
    }

    closeModal() {
        const {form} = this.props;
        this.setState({
            isSynWork: false
        });
        form.EmptyAllFormValue('partadd');
        form.EmptyAllFormValue('parttime');
        this.props.onClose && this.props.onClose();
    }

    onBeforeAddFormEvent(props, moduleId, key, value, index) {
        if (key === 'pk_psnjob') {
            let that = this;
            let meta = that.props.meta.getMeta();
            meta[moduleId].items.find(item => item.attrcode === key).queryCondition = function () {
                return {
                    "pk_org": that.props.orgVal && that.props.orgVal.refpk,
                    "GridRefActionExt": "nccloud.web.hrtrn.partrds.sqlbuilder.PartAddPsnRefSqlBuilder"
                }
            };
            that.props.meta.setMeta(meta);
        }
        return true;
    }

    onAfterAddFormEvent(props, moduleId, key, value, oldValue) {
        if (key === 'pk_psnjob') {
            afterPsnSelect.call(this, moduleId, key, value)
        }
    }

    onBeforePartFormEvent(props, moduleId, key, value, index) {
        return beforeFormEdit.call(this, moduleId, key)
    }

    onAfterPartFormEvent(props, moduleId, key, value, oldValue) {
        afterFormEdit.call(this, moduleId, key, value)
    }

    render() {
        const {form, visible} = this.props;
        return (
            <NCModal
                show={visible}
                size="lg"
                onHide={this.closeModal}
                onEntered={this.getEditInitInfo}
                backdrop={'static'}
                className={'part-time-add'}
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {this.props.json['hi6007-000100']}{/* 国际化处理： 兼职人员*/}
                </NCModalHeader>
                <NCModalBody>
                    {form.createForm('partadd', {
                        onBeforeEvent: this.onBeforeAddFormEvent,
                        onAfterEvent: this.onAfterAddFormEvent
                    })}
                    {form.createForm('parttime', {
                        onBeforeEvent: this.onBeforePartFormEvent,
                        onAfterEvent: this.onAfterPartFormEvent
                    })}
                    <div className='lightapp-component-form my-form'>
                        <div className='form-item'>
                            <div
                                className='form-item-label'>{this.props.json['hi6007-000101']}{/* 国际化处理： 同步工作履历*/}</div>
                            <div className='form-item-control'>
                                <div className='form-component-item-wrapper checkbox_switch-wrapper edit'>
                                    <NCCheckbox
                                        checked={this.state.isSynWork}
                                        color="primary"
                                        onChange={(value) => {
                                            this.setState({
                                                isSynWork: value
                                            })
                                        }}
                                    />
                                </div>
                            </div>
                        </div>
                    </div>
                </NCModalBody>
                <NCModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.beSure}
                    >
                        {this.props.json['hi6007-000174']}{/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton
                        shape="border"
                        onClick={this.closeModal}
                    >
                        {this.props.json['hi6007-000005']}{/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default AddNew;
